import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/services/userService.dart';
import 'package:xiaomi_shop/app/services/xhttp.dart';
import 'package:xiaomi_shop/models/index.dart';

class RegisterStep3Controller extends GetxController {
  var phone = Get.arguments["tel"];
  var code = Get.arguments["code"];

  final TextEditingController passwordTextController = TextEditingController();
  final TextEditingController enterPasswordTextController =
      TextEditingController();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  /**
   * 1、注册成功后将用户信息保存
   * 2、保存后跳转至 tab 的个人中心页面
   * 3、tab页面的处理
   * 4、个人中心页面判断是否登录
   * 5、退出登录
   */
  Future<bool> register() async {
    var response = await XHttp.getInstance().post(Api.register,
        {"tel": phone, "code": code, "password": passwordTextController.text});
    if (response != null) {
      Result result = response as Result;
      if (result.data["success"]) {
        List userinfos = result.data["userinfo"];
        UserService.saveUser(userinfos.first);
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }
}
